#include "MainMenuTask.h"
                                                  
__task void TaskMainMenu (void){
    char *available_menu[4] = {
        "Set Time ",
        "Set Alarm",
        "Stopwatch",
    };      
    char buffer[20];
    int maxmenu = 3;
    int curmenu = 0;
    int showmenu=0;
    int x,last=0;  
     
    OpenKeyPad();

    while (1){
        if (showmenu&&curmenu<maxmenu&&curmenu>=0){
            sprintf(buffer,"<<  %s >>", available_menu[curmenu]);
            print_lcd(LCD_MENU_POS,buffer);
        }

        x = Scankey();  
        //penekanan tombol terus menerus tidak dihiraukan
        if (last) {last=x;x=0;}else{last=x;}


        if (x) {
            if(showmenu){
                switch (x){
                    case LEFT:
                        curmenu=(curmenu==0)?maxmenu-1:--curmenu;
                        break;
                    case RIGHT:
                        curmenu=(curmenu==(maxmenu-1))?0:++curmenu;
                        break;
                    case OK:
                        showmenu = 0;
                        switch (curmenu){
                            case 0:
                                print_lcd(LCD_MENU_POS,"                ");
                                os_tsk_create(TaskEditTime,   40);
                                break;
                            case 1:
                                print_lcd(LCD_MENU_POS,"                ");
                                os_tsk_create(TaskEditAlarm,   50);
                                break;
                            case 2:
                                print_lcd(LCD_MENU_POS,"                ");
                                //os_tsk_create(TaskStopwatch,   40);
                                break;
                        }
                        //curmenu = -1;
                        break;
                    case BACK:
                        print_lcd(LCD_MENU_POS,"                ");
                        showmenu = 0; 
                        //curmenu = -1;
                        break;
                           
                }
            }
            else {
                showmenu=1;
            }
        }    
    }   
}
